package com.neusoft.service.impl;

import com.neusoft.mapper.CreditMapper;
import com.neusoft.po.Credit;
import com.neusoft.service.CreditService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CreditServiceImpl implements CreditService {

    @Autowired
    private CreditMapper creditMapper;

    //为用户增加一个钱包
    public int insertCredit(String userId){
        return creditMapper.insertCredit(userId);
    }

    //通过用户ID获得所持有的积分总数
    public int getTotalCreditByUserId(String userId){
        return creditMapper.getTotalCreditByUserId(userId);
    }

    //使用积分，使得积分减少
    public int payoutByUserId(String userId,Integer sub_number){
        if(sub_number<0){return -2;}
        int number=creditMapper.getTotalCreditByUserId(userId)-sub_number;
        return creditMapper.updateCreditNumberByUserId(userId,number);
    }

    //增加积分
    public int rechargeByUserId(String userId,Integer number){
        if(number<0){return -2;}
        number=creditMapper.getTotalCreditByUserId(userId)+number;
        return creditMapper.updateCreditNumberByUserId(userId,number);
    }

}
